﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 花店管理系统
{
    public partial class Tshoppingcar
    {
        DataClasses1DataContext db = new DataClasses1DataContext();

        public Tshoppingcars Add(int customerId, int  productId, int qty)
        {
            Tshoppingcars tshoppingcars = null;

            Tgood tgood= (from p in db.Tgood
                               where p.G_id== productId
                          select p).First();
            //当前需要添加的Tshopcart购物车对象
            tshoppingcars = new Tshoppingcars();
            tshoppingcars.t_id = customerId;
            tshoppingcars.G_id = tgood.G_id;
            tshoppingcars.G_name = tgood.G_name;
            tshoppingcars.jiage = tgood.G_xiaoshoujia;
            tshoppingcars.chebenjia = tgood.G_chenbenjia;
            tshoppingcars.kuchun = qty;

            //如果客户当前商品已在购物车内，则只需要修改数量即可
            int ExistTshoppingcarsCount = (from c in db.Tshoppingcars
                                          where c.t_id == customerId && c.G_id == productId
                                          select c).Count();
            if (ExistTshoppingcarsCount > 0)//修改
            {
                Tshoppingcars existTshoppingcars = (from c in db.Tshoppingcars
                                                    where c.t_id == customerId && c.G_id == productId
                                                  select c).First();
                existTshoppingcars.kuchun += qty;//添加
            }
            else
            {
                db.Tshoppingcars.InsertOnSubmit(tshoppingcars);
            }

            db.SubmitChanges();
            return tshoppingcars;
        }
        public (decimal, decimal) GetTotalPriceByCustomerId(int customerId)//计算总价
        {
            List<Tshoppingcars> list = (from c in db.Tshoppingcars
                                        where c.t_id == customerId
                                       select c).ToList();

            return ((decimal)list.Sum(c => c.jiage * c.kuchun), (decimal)list.Sum(c => (c.jiage - c.chebenjia)));
        }
    }
    

}
